#include <iostream>
#include <string>
#include <stdio.h>
#include <mxml.h>  

using namespace std;

#define SRC_FILE "SampleApp.xml"
#define DST_FILE "list.txt"

#define PROJ_DIR "C:\\Texas Instruments\\ZStack-CC2530-2.5.1a\\Projects\\zstack\\Samples\\SampleApp\\CC2530DB"
#define TOOLKIT_DIR "C:\\Program Files\\IAR Systems\\Embedded Workbench 6.0 Evaluation\\8051"


int main()  
{  
    FILE *fsrc,*fdst;  
    mxml_node_t *tree;  
    string cpath;
    char buffer[1024];



    fsrc = fopen(SRC_FILE, "r");  
    if (fsrc==NULL)
    {
        cout << "src file open failure" << endl;
        return -1;
    }
    fdst = fopen(DST_FILE,"w");
    if (fdst==NULL)
    {
        cout << "dst file create failure" << endl;
        fclose(fsrc);
        return -1;
    }
    tree = mxmlLoadFile(NULL, fsrc,MXML_TEXT_CALLBACK);  

    mxml_node_t *project;      
    project = mxmlFindElement(tree, tree, "project",NULL, NULL,MXML_DESCEND);  
     
  
    mxml_node_t *configuration;  
    configuration = mxmlFindElement(project, tree, "configuration",NULL, NULL,MXML_DESCEND);  

    mxml_node_t *name;  
    name = mxmlFindElement(configuration, tree, "name",NULL, NULL,MXML_DESCEND);  
    printf("name == [%s]\n",name->child->value.text.string); 

    mxml_node_t *outputs;  
    outputs = mxmlFindElement(configuration, tree, "outputs",NULL, NULL,MXML_DESCEND);  


    mxml_node_t *file;
    for(file = mxmlFindElement(outputs, tree, "file",NULL, NULL,MXML_DESCEND_FIRST); \
        file != NULL;\
        file = mxmlFindElement(file, tree, "file",NULL, NULL,MXML_NO_DESCEND))
    {
        int pos,len;
        string tmp;
        //cout<<"path:"<<mxmlGetText(file,NULL)<<endl;
        bzero(buffer,1024);
        snprintf(buffer,1024,"%s",mxmlGetText(file,NULL));
        cpath.assign(buffer);

        pos = cpath.rfind('.', string::npos)+1;//从字符串str12反向开始查找字符s在字符串中的位置
        len = cpath.length(); 
        tmp  = cpath.substr(pos,len-pos);
        if((tmp == "cpp")||(tmp == "c")||(tmp == "h"))
        {
            //字符串处理
            if(0 == cpath.find("$PROJ_DIR$",0))
            {
                cpath.replace(0, 10, PROJ_DIR); //删除从0开始的3个字符，然后在0处插入字符串“qqqq” 
            }
            else if(0 == cpath.find("$TOOLKIT_DIR$",0))
            {
                cpath.replace(0, 13, TOOLKIT_DIR);
            }
            else
            {
                continue;
            } 
        }
        else
        {
            continue;
        }
        

        cout << cpath << endl;
        fprintf (fdst, "%s\n",cpath.c_str());
    }
        
    mxmlDelete(tree);  
    printf("success!\n");

    fclose(fsrc);
    fclose(fdst);  
    return 0 ;  
}  
